
/**
 * ----------------------------------------------------------------------------------------------------------------------
 * 初始化对象
 */
var yuan = {
	animation: true, 	// 点击菜单是否有动画效果
	htmlType: 1, 		// 1使用html页面，2引入html内容
	tabPage: true, 		// 标签页是否开启
	logo: "", 			// 项目名称
	homeUrl:"", 		// 默认首页
	data:null,			// 数据
	menuScrollbar:false,// 左侧菜单是否开启垂直滚动条，默认false，设置为true后鼠标放上出现滚动条
	menuType: 2,		// 菜单方式：1头部显示顶级左侧显示二级  2左侧显示所有菜单
	footer: false, 		// 是否开启页脚
	thisStyle: 3, 		// 选中菜单样式：1显示左侧线条 2显示右侧线条 3更改字体颜色
	logoType: 1,		// logo类型（默认1）：1跟随左侧菜单 2 固定头部
	topIconArray: [		// 箭头图标默认下标0
		{
			top:'&#xe652;', 
			bottom: '&#xe651;'
		},
		{
			top:'&#xe655;', 
			bottom: '&#xe654;'
		},
		{
			top:'&#xe620;', 
			bottom: '&#xe623;',
			bottom2: '&#xe620;'
		},
	], 
	topIconIndex:0,		// 箭头图标 0~1
	loadMenus: function(data){
		
		// 标签初始化
		var s = '<a onclick="yuan.showHidenMenu(this)" class="yuna-head-menu-show-hiden">';
		s += '	<i class="yuan-icon yuan-left-icon" yuan-id="1">&#xe622;</i>';
		s += '</a>';
		s += '<ul class="yuan-head-menu-ul"></ul>';
		$(".yuan-head-center").append(s);
		
		if(data.hasOwnProperty("menuScrollbar")){
			yuan.menuScrollbar = data.menuScrollbar;
		}
		if(yuan.menuScrollbar){
			$(".yuan-left-tree").addClass("yuan-left-tree-scrollbar");
		}
		if(data.hasOwnProperty("topIconIndex")){
			if((data.topIconIndex+"").length > 0 && Number(data.topIconIndex) < yuan.topIconArray.length && Number(data.topIconIndex) >= 0){
				yuan.topIconIndex = data.topIconIndex;
			}
		}
		if(data.hasOwnProperty("logoType")){
			yuan.logoType = data.logoType;
		}
		if(data.hasOwnProperty("animation")){
			yuan.animation = data.animation;
		}
		if(data.hasOwnProperty("menuType")){
			yuan.menuType = data.menuType;
		}
		if(data.hasOwnProperty("htmlType")){
			yuan.htmlType = data.htmlType;
		}
		if(data.hasOwnProperty("tabPage")){
			yuan.tabPage = data.tabPage;
		}
		if(data.hasOwnProperty("thisStyle")){
			yuan.thisStyle = data.thisStyle;
		}
		if(yuan.animation){
			$(" .yuan-head-logo,.yuan-head-center, .yuan-left, .yuan-left-menu-ul, .yuan-body").addClass("yuan-animation");
		}else{
			$(" .yuan-head-logo,.yuan-head-center, .yuan-left, .yuan-left ul, .yuan-body").removeClass("yuan-animation");
		}
		if(yuan.logoType == 2){
			$(".yuan-head-logo").addClass("yuan-head-logo2").removeClass("yuan-head-logo");
			$(".yuan-head-center").addClass("yuan-head-center2").removeClass("yuan-head-center");
		}
		
		
		if(data.hasOwnProperty("data")){
			yuan.showMenus(data.data);
			yuan.data = data.data;
		}
		
		
		
		/**
		 * ----------------------------------------------------------------------------------------------------------------------
		 * 头部-选择菜单
		 */
		$(".yuan-head-menu-ul>li").on("click", function(){
			yuan.homeUrl = "";
			var y_$this = $(this);
			$(".yuan-head-menu-this").removeClass('yuan-head-menu-this');
			y_$this.addClass("yuan-head-menu-this");
			var y_id = y_$this.attr("yuan-id");
			var list = yuan.data;
			var obj;
			for(var i=0; i<list.length; i++){
				obj = list[i];
				if(obj.mid == y_id){
					if(!yuan.homeUrl && obj.home){
						yuan.homeUrl = obj.url;
					}else{
						if(obj.children && obj.children.length > 0){
							yuan.initMenus(obj.children);
						}else{
							$(".yuan-left-menu-ul").empty();
						}
					}
					break;
				}
			}
			// 头部菜单-如果没有设置首页，则自动获取第一个头部菜单的左侧菜单一个菜单（如果此菜单有下级则拿下级做首页）
			if(!yuan.homeUrl){
				var mid = obj.mid;
				if(obj.children && obj.children.length > 0){
					mid = yuan.getHomePageMenus(obj.children);
				}else{
					yuan.homeUrl = obj.url;
				}
			}
			$(".yuan-body-iframe").attr("src", yuan.homeUrl);
			$("#menu-id-"+mid).addClass("yuan-this"+yuan.thisStyle);
		})
		
		
		
	},
	/**
	 * ----------------------------------------------------------------------------------------------------------------------
	 * 头部-展开/收起菜单
	 * @param {Object} $this
	 */
	showHidenMenu: function ($this){
		var y_id = $($this).find("i").attr("yuan-id");
		if(y_id == 1){
			$(".yuan-head-logo").css("width", "55px");
			$(".yuan-head-center").css("left", "55px");
			$(".yuan-left").css("width", "55px");
			$(".yuan-body").css("left", "55px");
			if(yuan.logoType == 1){
				$(".yuan-head-logo-text").css("display", "none");
			}
			$(".yuan-left-menu-icon").css("display", "none");
			$(".yuan-left-menu-title").find("span").css("display", "none");
			// 菜单收起操作
			$(".yuan-left-menu-title").each(function(){
				var y_$this = $(this);
				var y_icon = y_$this.find(".yuan-left-menu-icon");
				if(yuan.topIconIndex == 2){
					y_icon = y_$this.find(".yuan-left-menu-icon2");
				}
				if(y_icon && y_icon.length > 0){
					var y_id = y_icon.attr("yuan-id");
					if(y_id == 2){
						if(yuan.animation){
							y_icon.removeClass("yuan-left-menu-icon-rotate");
							var y_li = y_$this.next(".yuan-left-menu-line").children("li");
							var temp_height = $(y_li[0]).height()
							var y_height = y_li.length * temp_height;
							var y_temp_inter = setInterval(function(){
								y_height = y_height - 5;
								y_$this.next(".yuan-left-menu-line").css("height", y_height + "px");
								if(y_height <= 0){
									clearInterval(y_temp_inter);
									y_$this.next(".yuan-left-menu-line").css("height", "0px");
									y_$this.next(".yuan-left-menu-line").css("display", "none");
								}
							}, 10)
							y_icon.attr("yuan-id", 1);
						}else{
							y_icon.html(yuan.topIconArray[yuan.topIconIndex].bottom);
							y_$this.next(".yuan-left-menu-line").css("display", "none");
							y_$this.next(".yuan-left-menu-line").css("height", "0px");
							y_icon.attr("yuan-id", 1);
						}
					}
				}
			})
			
			$($this).find("i").html("&#xe624;");
			$($this).find("i").attr("yuan-id", 2);
		}else{
			$(".yuan-head-logo").css("width", "200px");
			$(".yuan-head-center").css("left", "200px");
			$(".yuan-left").css("width", "200px");
			$(".yuan-body").css("left", "200px");
			if(yuan.logoType == 1){
				$(".yuan-head-logo-text").css("display", "inline-block");
			}
			$(".yuan-left-menu-icon").css("display", "inline-block");
			$(".yuan-left-menu-title").find("span").css("display", "inline-block");
			// 展开菜单后显示最后选中菜单
			$(".yuan-left-menu-title").each(function(){
				var y_$this = $(this);
				var y_class = y_$this.attr("class");
				if(y_class.indexOf("yuan-this") != -1){
					yuan.openThisParent(y_$this);
					return false;
				}
			})
			
			$($this).find("i").html("&#xe622;");
			$($this).find("i").attr("yuan-id", 1);
		}
	},
	/**
	 * ----------------------------------------------------------------------------------------------------------------------
	 * 左侧-选中菜单的所有父级展开
	 * @param {Object} $this
	 */
	openThisParent: function ($this){
		var y_$this = $($this).parent().parent().prev(".yuan-left-menu-title");
		var y_icon = y_$this.find(".yuan-left-menu-icon");
		if(y_$this && y_$this.length > 0){
			if(y_icon && y_icon.length > 0){
				var y_id = y_icon.attr("yuan-id");
				if(yuan.animation){
					y_icon.addClass("yuan-left-menu-icon-rotate");
					y_$this.next(".yuan-left-menu-line").css("display", "block");
					var y_li = y_$this.next(".yuan-left-menu-line").children("li");
					var temp_height = $(y_li[0]).height()
					var y_height = y_li.length * temp_height;
					var y_temp_height = 0;
					var y_temp_inter = setInterval(function(){
						y_temp_height = y_temp_height + 5;
						y_$this.next(".yuan-left-menu-line").css("height", y_temp_height + "px");
						if(y_temp_height >= y_height){
							clearInterval(y_temp_inter);
							y_$this.next(".yuan-left-menu-line").css("height", "100%");
						}
					}, 10)
					y_icon.attr("yuan-id", 2);
				}else{
					y_icon.html("&#xe652;");
					y_$this.next(".yuan-left-menu-line").css("display", "block");
					y_$this.next(".yuan-left-menu-line").css("height", "100%");
					y_icon.attr("yuan-id", 2);
				}
			}
			yuan.openThisParent(y_$this);
		}
	},
	/**
	 * ----------------------------------------------------------------------------------------------------------------------
	 * 左侧-菜单事件
	 */
	leftMenuOn: function(){
		// 点击事件
		$(".yuan-left-menu-title").on("click", function(){
			var y_$this = $(this);
			// 是否存在下级，存在下级则不打开页面
			var y_icon = y_$this.find(".yuan-left-menu-icon");
			if(y_icon && y_icon.length > 0){
				// 是否是点击头部左侧菜单收起：1是 2否
				var y_s_h = $(".yuna-head-menu-show-hiden").find("i").attr("yuan-id");
				if(y_s_h == 1){
					var y_id = y_icon.attr("yuan-id");
					if(yuan.animation){
						if(y_id == 1){
							y_icon.addClass("yuan-left-menu-icon-rotate");
							y_$this.next(".yuan-left-menu-line").css("display", "block");
							var y_li = y_$this.next(".yuan-left-menu-line").children("li");
							var temp_height = $(y_li[0]).height()
							var y_height = y_li.length * temp_height;
							var y_temp_height = 0;
							var y_temp_inter = setInterval(function(){
								y_temp_height = y_temp_height + 5;
								y_$this.next(".yuan-left-menu-line").css("height", y_temp_height + "px");
								if(y_temp_height >= y_height){
									clearInterval(y_temp_inter);
									y_$this.next(".yuan-left-menu-line").css("height", "100%");
								}
							}, 10)
							y_icon.attr("yuan-id", 2);
						}else{
							y_icon.removeClass("yuan-left-menu-icon-rotate");
							var y_li = y_$this.next(".yuan-left-menu-line").children("li");
							var temp_height = $(y_li[0]).height()
							var y_height = y_li.length * temp_height;
							var y_temp_inter = setInterval(function(){
								y_height = y_height - 5;
								y_$this.next(".yuan-left-menu-line").css("height", y_height + "px");
								if(y_height <= 0){
									clearInterval(y_temp_inter);
									y_$this.next(".yuan-left-menu-line").css("height", "0px");
									y_$this.next(".yuan-left-menu-line").css("display", "none");
								}
							}, 10)
							y_icon.attr("yuan-id", 1);
						}
					}
				}else{
					if(y_id == 1){
						y_icon.html(yuan.topIconArray[yuan.topIconIndex].top);
						y_$this.next(".yuan-left-menu-line").css("display", "block");
						y_$this.next(".yuan-left-menu-line").css("height", "100%");
						y_icon.attr("yuan-id", 2);
					}else{
						y_icon.html(yuan.topIconArray[yuan.topIconIndex].bottom);
						y_$this.next(".yuan-left-menu-line").css("display", "none");
						y_$this.next(".yuan-left-menu-line").css("height", "0px");
						y_icon.attr("yuan-id", 1);
					}
				}
			}else{
				if(yuan.htmlType == 1){
					var y_url = y_$this.attr("yuan-url");
					$(".yuan-body-iframe").attr("src", y_url);
					$(".yuan-this"+yuan.thisStyle).removeClass("yuan-this"+yuan.thisStyle);
					y_$this.addClass("yuan-this"+yuan.thisStyle);
				}else if(yuan.htmlType == 2){
					//var y_url = y_$this.attr("yuan-url");
					$(".yuan-body-iframe").load('home2.html');
				}
				
			}
		})
		// 鼠标放入事件
		$(".yuan-left-menu-title").on("mouseover", function(ev){
			// 防止子级元素阻挡事件造成失效
			var oEvent = ev || window.event;
            if(toAffect(this, oEvent, 'mouseover')){
            	console.log("移入")
                var y_$this = $(this);
				var y_s_h = $(".yuna-head-menu-show-hiden").find("i").attr("yuan-id");
				if(y_s_h == 2){
					var y_children = y_$this.attr("yuan-children");
					if(y_children){
						
					}else{
						$(".yuan-tips").remove();
						var y_title = y_$this.attr("yuan-title");
						console.log(y_title)
						var offset = y_$this.offset();
						var w = $(".yuan-left").width();
						var h = $(".yuan-left").height();
						var s = '';
						if(offset.left < 20){
							s = '<div class="yuan-tips" style="left: '+(w + 10)+'px; top: '+(offset.top)+'px">';
							s += '	<div class="yuan-tips-left"></div>';
						}
						s += '	<span class="yuan-tips-text">'+y_title+'</span>';
						s += '</div>';
						$("body").append(s);
						$(".yuan-tips").fadeIn();
					}
				}
            }
		})
		// 鼠标移开事件
		$(".yuan-left-menu-title").on("mouseout", function(ev){
			// 防止子级元素阻挡事件造成失效
			var oEvent = ev || window.event;
            if(toAffect(this, oEvent, 'mouseout')){
            	console.log("移出")
				var y_$this = $(this);
				var y_s_h = $(".yuna-head-menu-show-hiden").find("i").attr("yuan-id");
				if(y_s_h == 2){
					var y_children = y_$this.attr("yuan-children");
					if(y_children){
						
					}else{
						$(".yuan-tips").fadeOut();
					}
				}
			}
		})
	},
	/**
	 * ----------------------------------------------------------------------------------------------------------------------
	 * 左侧-初始化显示菜单
	 * @param list
	 */
	showMenus: function(list){
		if(list && list.length > 0){
			if(yuan.menuType == 1){
				for(var i=0; i<list.length; i++){
					var obj = list[i];
					var y_str = '';
					y_str += '<li yuan-id="'+obj.mid+'" id="menu-li-'+obj.mid+'">';
					y_str += (obj.icon || "");
					y_str += '		<span>'+obj.mname+'</span>';
					y_str += '</li>';
					$(".yuan-head-menu-ul").append(y_str);
				}
				// 头部菜单-如果没有设置首页，则自动获取第一个头部菜单的左侧菜单一个菜单（如果此菜单有下级则拿下级做首页）
				if(!yuan.homeUrl){
					var mid,parentMid;
					for(var i=0; i<list.length; i++){
						var obj = list[i];
						if(obj.children && obj.children.length > 0 && !yuan.homeUrl){
							mid = yuan.getHomePage(obj.children);
							if(mid){
								parentMid = obj.mid;
								yuan.initMenus(obj.children);
								break;
							}
						}else if(obj.home && !yuan.homeUrl){
							yuan.homeUrl = obj.url;
							mid = obj.mid;
							parentMid = obj.mid;
							break;
						}
					}
					if(!yuan.homeUrl && !mid){
						var obj = list[0];
						mid = obj.mid;
						if(obj.children && obj.children.length > 0){
							mid = yuan.getHomePageMenus(obj.children);
							yuan.initMenus(obj.children);
						}else{
							yuan.homeUrl = obj.url;
						}
						parentMid = obj.mid;
					}
					yuan.openThisParent($("#menu-id-"+mid));
				}
				$(".yuan-body-iframe").attr("src", yuan.homeUrl);
				$("#menu-li-"+parentMid).addClass("yuan-head-menu-this");
				$("#menu-id-"+mid).addClass("yuan-this"+yuan.thisStyle);
			}else{
				yuan.initMenus(list);
				// 头部菜单-如果没有设置首页，则自动获取第一个头部菜单的左侧菜单一个菜单（如果此菜单有下级则拿下级做首页）
				if(!yuan.homeUrl){
					var mid,parentMid;
					for(var i=0; i<list.length; i++){
						var obj = list[i];
						if(obj.children && obj.children.length > 0 && !yuan.homeUrl){
							mid = yuan.getHomePage(obj.children);
							if(mid){
								parentMid = obj.mid;
								break;
							}
						}else if(obj.home && !yuan.homeUrl){
							yuan.homeUrl = obj.url;
							mid = obj.mid;
							parentMid = obj.mid;
							break;
						}
					}
					if(!yuan.homeUrl && !mid){
						var obj = list[0];
						mid = obj.mid;
						if(obj.children && obj.children.length > 0){
							mid = yuan.getHomePageMenus(obj.children);
						}else{
							yuan.homeUrl = obj.url;
						}
						parentMid = obj.mid;
					}
					yuan.openThisParent($("#menu-id-"+mid));
				}
				$(".yuan-body-iframe").attr("src", yuan.homeUrl);
				$("#menu-id-"+mid).addClass("yuan-this"+yuan.thisStyle);
			}
		}
	},
	/**
	 * ----------------------------------------------------------------------------------------------------------------------
	 * 左侧-菜单自动设置首页获取第一个菜单
	 * @param {Object} list
	 */
	getHomePageMenus: function(list){
		for(var i=0; i<list.length; i++){
			var obj = list[i];
			if(obj.children && obj.children.length > 0){
				return yuan.getHomePageMenus(obj.children);
			}else{
				yuan.homeUrl = obj.url;
				return obj.mid;
			}
		}
	},
	/**
	 * ----------------------------------------------------------------------------------------------------------------------
	 * 左侧-菜单查询设置首页菜单
	 * @param {Object} list
	 */
	getHomePage: function(list){
		for(var i=0; i<list.length; i++){
			var obj = list[i];
			if(obj.children && obj.children.length > 0){
				return yuan.getHomePage(obj.children);
			}else if(obj.home){
				yuan.homeUrl = obj.url;
				return obj.mid;
			}
		}
	},
	/**
	 * ----------------------------------------------------------------------------------------------------------------------
	 * 左侧-菜单左侧一级
	 * @param list
	 */
	initMenus: function(list){
		$(".yuan-left-tree").empty();
		if(list && list.length > 0){
			$(".yuan-left-tree").html('<ul class="yuan-left-menu yuan-left-menu-ul"></ul>');
			for(var i=0; i<list.length; i++){
				var obj = list[i];
				var y_str = '<li class="yuan-left-menu">';
				if(obj.children && obj.children.length > 0){
					y_str += '<div class="yuan-left-menu-title yuan-left-menu-title'+yuan.thisStyle+'" yuan-children="1" id="menu-id-'+obj.mid+'">';
					y_str += (obj.icon || "");
					y_str += '		<span>'+obj.mname+'</span>';
					if(yuan.topIconIndex == 2 && yuan.animation){
						y_str += '<i class="yuan-icon yuan-left-menu-icon" yuan-id="1">'+yuan.topIconArray[yuan.topIconIndex].bottom2+'</i>';
						y_str += '<i class="yuan-icon yuan-left-menu-icon yuan-left-menu-icon2" yuan-id="1">'+yuan.topIconArray[yuan.topIconIndex].bottom2+'</i>';
					}else{
						y_str += '<i class="yuan-icon yuan-left-menu-icon" yuan-id="1">'+yuan.topIconArray[yuan.topIconIndex].bottom+'</i>';
					}
					y_str += '	</div>';
				}else{
					y_str += '<div class="yuan-left-menu-title yuan-left-menu-title'+yuan.thisStyle+'" yuan-title="'+obj.mname+'" id="menu-id-'+obj.mid+'" yuan-url="'+obj.url+'">';
					y_str += (obj.icon || "");
					y_str += '		<span>'+obj.mname+'</span>';
					y_str += '	</div>';
				}
				y_str += yuan.initChildrenMenus(obj.children, 40);
				y_str += '</li>';
				$(".yuan-left-menu-ul").append(y_str);
			}
		}
		// 刷新左侧菜单事件
		yuan.leftMenuOn();
	},
	/**
	 * ----------------------------------------------------------------------------------------------------------------------
	 * 左侧-菜单子级
	 * @param 
	 */
	initChildrenMenus: function(list, leftNum){
		var y_str = "";
		if(list && list.length > 0){
			y_str = '<ul class="yuan-left-menu yuan-left-menu-line">';
			for(var i=0; i<list.length; i++){
				var obj = list[i];
				y_str += '<li class="yuan-left-menu">';
				if(obj.children && obj.children.length > 0){
					y_str += '<div class="yuan-left-menu-title yuan-left-menu-title'+yuan.thisStyle+'" id="menu-id-'+obj.mid+'" style="padding-left: '+leftNum+'px;">';
					y_str += (obj.icon || "");
					y_str += '		<span>'+obj.mname+'</span>';
					if(yuan.topIconIndex == 2 && yuan.animation){
						y_str += '<i class="yuan-icon yuan-left-menu-icon" yuan-id="1">'+yuan.topIconArray[yuan.topIconIndex].bottom2+'</i>';
						y_str += '<i class="yuan-icon yuan-left-menu-icon yuan-left-menu-icon2" yuan-id="1">'+yuan.topIconArray[yuan.topIconIndex].bottom2+'</i>';
					}else{
						y_str += '<i class="yuan-icon yuan-left-menu-icon" yuan-id="1">'+yuan.topIconArray[yuan.topIconIndex].bottom+'</i>';
					}
					y_str += '	</div>';
				}else{
					y_str += '<div class="yuan-left-menu-title yuan-left-menu-title'+yuan.thisStyle+'" id="menu-id-'+obj.mid+'" style="padding-left: '+leftNum+'px;" yuan-url="'+obj.url+'">';
					y_str += (obj.icon || "");
					y_str += '		<span>'+obj.mname+'</span>';
					y_str += '	</div>';
				}
				y_str += yuan.initChildrenMenus(obj.children, leftNum + 20);
				y_str += '</li>';
			}
			y_str += "</ul>";
		}
		return y_str;
	},
}



/**
 * -------------------------------------------------------------------------------------------------------------------------------------
 * 父级移入移出事件阻止子级元素遮挡后失效
 * @param {Object} obj1
 * @param {Object} ev
 * @param {Object} event
 */
function toAffect(obj1, ev, event){
    var obj2 = null;
    if(ev.relatedTarget){
        obj2 = ev.relatedTarget;
    }
    else{
        if(event == 'mouseover'){
            obj2 = ev.fromElement;
        }
        else if(event == 'mouseout'){
            obj2 = ev.toElement;
        }
    }
    if(obj1.contains){
        return !obj1.contains(obj2);
    }
    else{
        return !!(obj1.compareDocumentPosition(obj2)-20)&&a!=b;
    }
}












